package net.maku.project.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.project.convert.ProcessStagesConvert;
import net.maku.project.entity.ProcessStagesEntity;
import net.maku.project.service.ProcessStagesService;
import net.maku.project.query.ProcessStagesQuery;
import net.maku.project.vo.ProcessStagesVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

import java.util.List;

/**
 * 流程阶段表
 *
 * @author 艾力 1106997025@qq.com
 * @since 1.0.0 2024-07-09
 */
@RestController
@RequestMapping("project/process-stages")
@Tag(name = "流程阶段表")
@AllArgsConstructor
public class ProcessStagesController {
    private final ProcessStagesService processStagesService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('project:process-stages:page')")
    public Result<PageResult<ProcessStagesVO>> page(@ParameterObject @Valid ProcessStagesQuery query) {
        PageResult<ProcessStagesVO> page = processStagesService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('project:process-stages:info')")
    public Result<ProcessStagesVO> get(@PathVariable("id") Long id) {
        ProcessStagesEntity entity = processStagesService.getById(id);

        return Result.ok(ProcessStagesConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    @PreAuthorize("hasAuthority('project:process-stages:save')")
    public Result<String> save(@RequestBody ProcessStagesVO vo) {

        processStagesService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @PreAuthorize("hasAuthority('project:process-stages:update')")
    public Result<String> update(@RequestBody @Valid ProcessStagesVO vo) {
        processStagesService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @PreAuthorize("hasAuthority('project:process-stages:delete')")
    public Result<String> delete(@RequestBody List<Long> idList) {
        processStagesService.delete(idList);

        return Result.ok();
    }

    @GetMapping("/project/{projectId}")
    @Operation(summary = "根据项目编号查询")
    @PreAuthorize("hasAuthority('project:process-stages:list')")
    public Result<List<ProcessStagesVO>> getByProjectId(@PathVariable("projectId") Long projectId) {
        List<ProcessStagesVO> processStagesVOS = processStagesService.getByProjectId(projectId);
        return Result.ok(processStagesVOS);
    }
}